<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('开单')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
    <style>
        .table_input_s{
            width: 60px;
        }
        .table_input_s2{
            width: 90px;
        }
        .preimg{
            width: 70px;height: 70px;
        }
    </style>
</head>
<body class="gray-bg">
    <div class="main-content">
        <form id="form-add" name="myForm" class="form-horizontal">
            <input name="deptId" type="hidden" id="treeId"/>
<!--            <h4 class="form-header h4">扫条码</h4>-->
            <div class="row">
                <div class="col-sm-12">
                    <div class="form-group">
                        <label class="col-xs-2 control-label is-required">商品编号：</label>
                        <div class="col-xs-10">
                            <select id="styleNumber" name="styleNumber"></select>
                            <div style="margin-top: 5px">
                                <a class="btn btn-primary btn-rounded btn-sm"  id="btnAdd"><i class="fa fa-search"></i>&nbsp;添加</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <h4 class="form-header h4">商品购物车</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
					    <table id="bootstrap-table"></table>
					</div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <ul class="sortable-list connectList agile-list">
                <li class="warning-element">
                    总金额：￥<span id="totalMoney" style="color: #FF6600">0</span>
                </li>
            </ul>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i> 下 单</button>&nbsp;
            <!--<button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>-->
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript"> var ctx = [[@{/}]]; var captchaType = [[${captchaType}]]; </script>
    <script src="../../static/js/select.goods.lib.js" th:src="@{/js/select.goods.lib.js}"></script>

    <!--    <script th:src="@{/static/js/jquery.tmpl.js}"></script>-->
	<script th:inline="javascript">
	    $(function() {
            var datas = [[${@dict.getType('goods_colors')}]];
            var domainDownload=ctx + "common/download/resource?resource=";
		    var options = {
                pagination: false,
		        showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
		        columns: [
		            {
		                checkbox: true
                    },
                    {
                        field: 'index',
                        align: 'left',
                        title: "序号",
                        formatter: function (value, row, index) {
                            var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                            return columnIndex + $.table.serialNumber(index);
                        }
                    },
                    {
                        field: 'name',
                        align: 'left',
                        title: '商品名称',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input readonly class='form-control' type='text' name='goods[%s].name' value='%s' required:true>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'styleNumber',
                        align: 'left',
                        title: '款号',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input readonly class='form-control' type='text' name='goods[%s].styleNumber' value='%s' required:true>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'tagPrice',
                        align: 'left',
                        title: '吊牌价',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input id='tagPrice_"+index+"' readonly class='form-control table_input_s2' type='text' name='goods[%s].tagPrice' value='%s' placeholder='yyyy-MM-dd'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'preferentialPrice',
                        align: 'left',
                        title: '优惠价',
                        formatter: function(value, row, index) {
                            //var data = [{ index: index, type: value }];
                            //return $("#goodsTypeTpl").tmpl(data).html();
                            var html = $.common.sprintf("<input class='form-control table_input_s2' readonly type='text' name='goods[%s].preferentialPrice' value='%s'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'quantity',
                        align: 'left',
                        title: '商品数量',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input id='quantity_"+index+"' class='form-control table_input_s' type='number' onchange='changeRows(1,this.value,"+row.tagPrice+","+row.discount+","+row.quantity+","+index+")' name='goods[%s].quantity' value='%s' digits:true>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'discount',
                        align: 'left',
                        title: '门店折扣',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input id='discount_"+index+"' class='form-control table_input_s' type='number' name='goods[%s].discount' onChange='changeRows(2,this.value,"+row.tagPrice+","+row.discount+","+row.quantity+","+index+")' value='%s' title='吊牌价折扣'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'money',
                        align: 'left',
                        title: '金额',
                        formatter: function(value, row, index) {
                            //var data = [{ index: index, type: value }];
                            //return $("#goodsTypeTpl").tmpl(data).html();
                            var html = $.common.sprintf("<input id='money_"+index+"' class='form-control table_input_s2' readonly title='金额=吊牌价*门店折扣' style='color: red' type='text' name='goods[%s].money' value='%s'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'color',
                        align: 'left',
                        title: '颜色',
                        formatter: function(value, row, index) {
                            var x=$.table.selectDictLabel(datas,value);
                            var html = $.common.sprintf(x+"<input type='hidden' name='goods[%s].color' value='%s'>", index, value);
                            return html;
                        }
                    }
                    //, {
                    //     field: 'imgPath',
                    //     title: '图片',
                    //     formatter: function(value, row, index) {
                    //         console.log("test:"+value)
                    //         var html = $.common.sprintf("<input readonly class='form-control' type='text' name='goods[%s].imgPath' value='%s' required:true>", index, value);
                    //         return html;
                    //     }
                    // }
		        ]
		    };
		    $.table.init(options);

		    $("#styleNumber").focus().select();
		});

        function addColumn(name,styleNumber,quantity,tagPrice,discount,preferentialPrice,money,color,img) {
        	var count = $("#" + table.options.id).bootstrapTable('getData').length;
        	sub.editColumn();
        	console.log("增加一行"+img)
        	$("#" + table.options.id).bootstrapTable('insertRow', {
        		index: count,
                row: {
                	index: $.table.serialNumber(count),
                    name:name,
                    styleNumber:styleNumber,
                    quantity:quantity,
                    tagPrice:tagPrice,
                    discount:discount,
                    preferentialPrice:preferentialPrice,
                    money:money,
                    color:color,
                    imgPath:img
                }
        	});
        }

        $("#bootstrap-table").on("post-body.bs.table", function (e, args) {
        	$("input[name$='date']").datetimepicker({
	            format: "yyyy-mm-dd",
	            minView: "month",
	            autoclose: true,
	            pickerPosition:'top-right'
	        });
    	});

        $("#btnAdd").click(function (){
            var styleNumber=$("#styleNumber").val();
            if(styleNumber==''){
                $.modal.msgError("款号不能为空");
                return;
            }
            $.ajax({
                type: "post",
                url: ctx + "goods/goods/selectGoodsByKey/",
                data: {
                    "styleNumber": styleNumber,
                    "fragment":'fragment-tasklist'
                },
                success: function(data1) {
                    var data=data1.value;
                    //添加购物车
                    if(data==null || data=='null'){
                        $.modal.alertError('编号错误，未匹配到商品')
                    }else{
                        var money=data.tagPrice;
                        var quantity=1;
                        var discount=10;
                        var imgp=data.imgPath;
                        addColumn(data.name,data.styleNumber,quantity,data.tagPrice,discount,data.preferentialPrice,money,data.color,imgp);
                        calcTotalMoney();
                        //重新给条形码文本框焦点
                        $("#styleNumber").focus().select();
                    }
                }
            });


        });
        //计算折扣,改变行数据
        function changeRows(i,val,tagPrice,discount,quantity,index){
            var tp=$("#tagPrice_"+index).val();
            var d=$("#discount_"+index).val();
            var q=$("#quantity_"+index).val();
            var m=eval(tp*eval(d/10)*q);
            $("#money_"+index).val(m);
            calcTotalMoney();
        }

        //计算数量,改变行数据
        function changeRowsZk(val,tagPrice,quantity,index){
            $("input[name='goods["+index+"].money']").val(eval(tagPrice*val/10*quantity));
            calcTotalMoney();
        }
        //计算折扣,改变行数据
        function changeRowsZk(val,tagPrice,quantity,index){
            $("input[name='goods["+index+"].money']").val(eval(tagPrice*val/10*quantity));
            calcTotalMoney();
        }
        //重新计算总金额
        function calcTotalMoney(){
            var data = $("#form-add").serializeArray();
            var totalMoney=0;
            $.each(data, function(i, field){
                //$("#results").append(field.name + ":" + field.value + " ");
                if(field.name.indexOf("money")>-1){
                    totalMoney=eval(totalMoney+eval(field.value));
                }
            });
            $("#totalMoney").text(totalMoney);
        }

        /* 提交表单，下单 */
        function submitHandler(index, layero){
            var prefix2 = ctx + "goods/order"
            if ($.validate.form()) {
                $.modal.confirm("确认要下单吗？",function (){
                    $.operate.save(prefix2 + "/add", $("#form-add").serializeArray(),function (d){
                        var orderNo=d.orderNo;
                        $.modal.openTab('收银台'+orderNo, prefix2 + "/orderDetail/"+d.orderNo);
                        location.reload();
                    });
                });
            }
        }

        function successTabCallback(){
            console.log("毁掉函数")
        }


    </script>
</body>
</html>

<!-- 商品类型 -->
<script id="goodsTypeTpl" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='goods[${index}].type'>
    <option value="">所有</option>
    <option value="0" {{if type==="0"}}selected{{/if}}>寒性</option>
    <option value="1" {{if type==="1"}}selected{{/if}}>热性</option>
</select>
</div>
</script>